package com.webull.information.center.carwler.common.util.jsoup;

import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;

import com.webull.information.center.carwler.common.model.NewsInformation;

/**
 * JSOP解析工具类
 * 
 * @author shimingjun
 * @date 2016年8月24日 上午10:02:16
 * @version 1.0
 * @since JDK 1.8
 */
public class JsoupPraseUtil {

	/**
	 * 递归 将自身标签转换为纯文本标签 用于span/a标签等
	 * 
	 * @param a0
	 * @since 0.1.0
	 */
	public static void replaceWithText(Element a0, String tagName) {
		if (a0 == null || a0.parent() == null || StringUtils.isBlank(tagName))
			return;
		Optional.ofNullable(a0.select(tagName)).ifPresent(eles -> {
			eles.forEach(ele -> replaceWithText(ele));
		});
	}

	public static void removeBackground(Element p0) {
		String styleStr = p0.attr("style");
		if (StringUtils.isBlank(styleStr))
			return;

		int a1 = StringUtils.indexOfIgnoreCase(styleStr, "background");
		if (a1 < 0)
			return;

		p0.removeAttr("style");
		int a2 = StringUtils.indexOf(styleStr, ";", a1);
		if (a2 < 0)
			styleStr = styleStr.substring(0, a1);
		else
			styleStr = styleStr.substring(0, a1) + styleStr.substring(a2 + 1);
		p0.attr("style", styleStr);
	}

	public static void main(String[] args) {
		String a = ": justify; color: #555555; font-family: 'Microsoft YaHei'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff";
		System.out.println(StringUtils.substringBetween(a, "background", ";"));
	}

	public static void replaceWithText(Element a0) {
		/**
		 * a0.parent() == null
		 * 代表是孤立元素，说明在get之后,replaceWithText之前，parent被置空过。即已被替换
		 */
		if (a0 == null || a0.parent() == null)
			return;
		try {
			while (!a0.children().isEmpty()) {
				replaceWithText(a0.children().first());
			}
			a0.replaceWith(new TextNode(StringUtils.stripToEmpty(a0.ownText()), ""));
		} catch (Exception e) {

		}
	}

	/**
	 * 替换img标签为绝对地址,防止网站使用相对位置
	 * 
	 * @param a0
	 * @since 0.1.0
	 */
	public static void replaceImgAbsSrc(Element a0) {
		Optional.ofNullable(a0).map(a1 -> a1.select("img")).ifPresent(imgs -> {
			for (Element img : imgs) {
				img.removeAttr("style");
				img.attr("src", img.attr("abs:src"));
			}
		});
	}

	/**
	 * 去除元素标签内部文本 前后的空白符
	 * 
	 * @param p
	 * @since 0.1.0
	 */
	// ** 递归有待验证

	public static void trimParagraph(Element p) {
		try {
			if (p == null)
				return;
			// for (Element p0 : p.children()) {
			// trimParagraph(p0);
			// }
			p.html(StringUtils.stripToEmpty(p.html()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getMainPic(Element p) {
		return Optional.ofNullable(p.getElementsByTag("img"))//
				.filter(vs -> vs.size() >= 1)//
				.map(vs -> vs.get(0))//
				.filter(vs -> vs.hasAttr("src"))//
				.map(vs -> vs.absUrl("src")).orElse(null);
	}

	public static int addMainPic(Element p, NewsInformation info, int maxCount) {
		String mainpic = getMainPic(p);
		if (StringUtils.isNotBlank(mainpic) && info.getMainPic().getMainPic().size() < maxCount) {
			info.addMainPic(mainpic);
			return 1;
		}
		return 0;
	}

}
